#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/of_irq.h>
#include <asm/io.h>

/*---------------字符设备内容----------------------*/

#define DEV_NAME "button"
#define DEV_CNT (1)

//定义字符设备设备号
static dev_t button_devno;
//定义字符设备结构体
static struct cdev button_chr_dev;
//定义类指针
struct class* class_button;
//定义设备指针
struct device* device_button;

//定义按键设备节点结构体
struct device_node* button_device_node;
//保存button使用的GPIO引脚编号
unsigned button_gpio_number;
//button引脚中断编号
unsigned interrupt_number;
//定义原子变量保存按键状态
atomic_t button_status = ATOMIC_INIT(0);


struct tasklet_struct button_tasklet;

void button_tasklet_hander(unsigned long data)
{
    int counter = 1;
    mdelay(200);
    printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
    mdelay(200);
    printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
    mdelay(200);
    printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
    mdelay(200);
    printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
    mdelay(200);
    printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
}

static irqreturn_t button_irq_hander(int irq, void* dev_id)
{
    printk(KERN_ERR "button_irq_hander---------------exit\n");
    /*按键状态加一*/
    atomic_inc(&button_status);

    tasklet_schedule(&button_tasklet);
    
    printk(KERN_ERR "button_irq_hander--------------exit\n");
    return IRQ_HANDLED;
}

//open
static int button_open(struct inode* inode, struct file* filp)
{
    int error = 0;

    //获取按键 设备树节点
    button_device_node = of_find_node_by_path("/button_interrupt");
    if (NULL == button_device_node)
    {
        printk("of_find_node_by_path error:");
        return -1;
    }
    
    // 获取按键使用的GPIO
    button_gpio_number = of_get_named_gpio(button_device_node, "buttons-gpios", 0);
    if (0 == button_gpio_number)
    {
        printk("of_get_named_gpio error");
        return -1;
    }

    //申请gpio，记得释放
    error = gpio_request(button_gpio_number, "button_gpio");
    if (error < 0)
    {
        printk("gpio_request error");
        gpio_free(button_gpio_number);
        return -1;
    }

    //设置引脚为输入模式
    error = gpio_direction_input(button_gpio_number);
    
    //获取中断号
    interrupt_number = irq_of_parse_and_map(button_device_node, 0);
    printk("irq_of_parse_and_map =  %d \n", interrupt_number);

    //申请中断记得释放
    error = request_irq(interrupt_number, button_irq_hander, IRQF_TRIGGER_RISING, "button_interrupt", device_button);
    if (error != 0)
    {
        printk("request_irq error");
        free_irq(interrupt_number, device_button);
        return -1;
    }

    //初始化button_tasklet
    tasklet_init(&button_tasklet, button_tasklet_hander, 0);

    //申请之后已经开启了,切记不要再次打开，否则运行时报错
    //enable_irq(interrupt_number);

    return 0;
}

// read
static ssize_t button_read(struct file* filp, char __user* buf, size_t cnt, loff_t* offt)
{
    int error =0;
    int button_countervc = 0;

    //读取按键值状态
    button_countervc = atomic_read(&button_status);

    //将结果拷贝到用户空间
    error = copy_to_user(buf, &button_countervc, sizeof(button_countervc));
    if (error < 0)
    {
        printk("copy_to_user error");
        return 0;
    }

    //清楚按键值状态
    atomic_set(&button_status, 0);
    return 0;
}

//release
static int button_release(struct inode* inode, struct file* filp)
{
    //释放申请的引脚和中断
    free_irq(interrupt_number, device_button);
    gpio_free(button_gpio_number);
    return 0;
}

//字符操作函数集和
static struct file_operations button_chr_dev_fops = {
    .owner = THIS_MODULE,
    .open  = button_open,
    .read  = button_read,
    .release = button_release
};

//驱动初始化函数
static int __init button_driver_init(void)
{
    int error = 0;
    
    //采用动态分配设备号,次设备号为0
    error = alloc_chrdev_region(&button_devno, 0,  DEV_CNT, DEV_NAME);
    if (error < 0)
    {
        printk("fail to allco button_devno\n");
        goto alloc_err;
    }


    //关联字符设备结构体和文件操作函数
    button_chr_dev.owner = THIS_MODULE;
    cdev_init(&button_chr_dev, &button_chr_dev_fops);

    //添加设备到cedv_map中散列表中
    error = cdev_add(&button_chr_dev, button_devno, DEV_CNT);
    if (error < 0)
    {
        printk("fail to add cdev \n");
        goto add_err;
    }

    //创建类
    class_button = class_create(THIS_MODULE, DEV_NAME);
    //创建设备
    device_button = device_create(class_button, NULL,  button_devno, NULL, DEV_NAME);
    return 0;
   
alloc_err:
    return -1;


add_err:
    unregister_chrdev_region(button_devno, DEV_CNT);
    printk("cdev_add failed \n");
    return -1;
}

//驱动注销函数
static void __exit button_driver_exit(void)
{
    pr_info("button_driver_exit\n");
    //删除设备
    device_destroy(class_button, button_devno);
    //删除类
    class_destroy(class_button);
    //删除设备号
    cdev_del(&button_chr_dev);
    //注销字符设备
    unregister_chrdev_region(button_devno, DEV_CNT);
}





module_init(button_driver_init);
module_exit(button_driver_exit);
MODULE_LICENSE("GPL");